package edu.friday.controller;

import edu.friday.common.constant.Constants;
import edu.friday.common.result.RestResult;
import edu.friday.common.security.LoginUser;
import edu.friday.common.security.service.SysLoginService;
import edu.friday.common.security.service.SysPermissionService;
import edu.friday.common.security.service.TokenService;
import edu.friday.model.vo.SysMenuVO;
import edu.friday.model.vo.SysUserVO;
import edu.friday.service.SysMenuService;
import edu.friday.utils.http.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.Set;

@RestController
public class SysLoginController {
        @Autowired
        private SysLoginService loginService;
        @Autowired
        private TokenService tokenService;
        @Autowired
        private SysPermissionService sysPermissionService;
        @Autowired
        private SysMenuService menuService;
        @PostMapping({"login","/"})
        public RestResult login(String username ,String password,String uuid){
            RestResult ajax  = RestResult.success();
            String token = loginService.login(username,password,"123",uuid);
            ajax.put(Constants.TOKEN,token);
            return ajax;
        }
    @GetMapping("getInfo")
        public RestResult getInfo(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUserVO user = loginUser.getUser();
        Set<String> roles = sysPermissionService.getRolePermission(user);
        Set<String> permissions = sysPermissionService.getMenuPermission(user);
        RestResult ajax = RestResult.success();
        ajax.put("user",user);
        ajax.put("roles",roles);
        ajax.put("permissions",permissions);
        return  ajax;
    }
    @GetMapping("getRouters")
    public RestResult getRouters(){
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            SysUserVO user = loginUser.getUser();

        List<SysMenuVO> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return  RestResult.success(menuService.buildMenus(menus));
    }
}
